//
//  XHComposePictureView.swift
//  gu-新浪微博
//
//  Created by xiaohui on 16/7/4.
//  Copyright © 2016年 gusheghui. All rights reserved.
//

import UIKit
private let pictureCellId = "pictureCellId"
class XHComposePictureView: UICollectionView {
    
    //加号按钮的闭包 点击调用相册
    var showPicClosure:(()->())?
    
    var imageList:[UIImage] = [UIImage]()
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        hidden = true
        backgroundColor = UIColor.lightGrayColor()
        
        dataSource = self
        delegate = self
        
        //注册cell
        registerClass(XHComposePictureCell.self, forCellWithReuseIdentifier: pictureCellId)
        
        let layout = collectionViewLayout as? UICollectionViewFlowLayout
        let itemMargin :CGFloat = 5
        let cellWH = (SCREENW - 20 - itemMargin*2)/3
        layout?.itemSize = CGSize(width: cellWH, height: cellWH)
        layout?.minimumInteritemSpacing = itemMargin
        layout?.minimumLineSpacing = itemMargin
    }
    
    func addImages(image:UIImage){
        //        imageList.append(image)
        
        //添加图片到数组第一个
        imageList.insert(image, atIndex: 0)
        
        if imageList.count > 9{
            imageList.removeLast()
        }
        hidden = false
        reloadData()
    }
}

extension XHComposePictureView:UICollectionViewDataSource,UICollectionViewDelegate{
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        //        return imageList.count
        if imageList.count == 0 || imageList.count == 9{
            return imageList.count
        }else{
            
            return imageList.count + 1
            
        }
        
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(pictureCellId, forIndexPath: indexPath) as! XHComposePictureCell
        
        //        cell.backgroundColor = RandomColor()
        
        if imageList.count == indexPath.item{
            //如果是加号设置nil
            cell.image = nil
        }else{
            cell.image = imageList[indexPath.item]
        }
        
        //执行闭包
        cell.deletePicClosure = {[weak self] ()->()in
            self?.imageList.removeAtIndex(indexPath.item)
            if self?.imageList.count == 0{
                self?.hidden = true
            }
            self?.reloadData()
        }
        return cell
    }
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        
        collectionView.deselectItemAtIndexPath(indexPath, animated: true)
        //加号按钮的点击
        if imageList.count == indexPath.item{
            showPicClosure?()
        }
    }
}

class XHComposePictureCell:UICollectionViewCell{
    /*给cell设置图片
    0<indexpath.item <8 数组+1 添加一个加号图片
    
    indexpath.item = 0  || = 8 的时候不显示加号
    
    */
    //X号按钮的闭包声明  点击删除相册
    var deletePicClosure:(()->())?
    var image:UIImage?{
        didSet{
            if image == nil{
                //设置加号图片
                
                pictureView.image = UIImage(named: "compose_pic_add")
                pictureView.highlightedImage = UIImage(named: "compose_pic_add_highlighted")
                
                deleteButton.hidden = true
                
            }else{
                
                //设置图片
                pictureView.image = image
                pictureView.highlightedImage = nil
                deleteButton.hidden = false
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func deleteButtonClick(){
        //删除图片
        
        deletePicClosure?()
    }
    
    func setupUI(){
        contentView.addSubview(pictureView)
        contentView.addSubview(deleteButton)
        pictureView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        
        deleteButton.snp_makeConstraints { (make) -> Void in
            make.top.right.equalTo(pictureView)
        }
        
    }
    
    private lazy var pictureView:UIImageView = UIImageView()
    private lazy var deleteButton:UIButton = {
        
        let button = UIButton()
        
        button.addTarget(self, action: "deleteButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        button.setImage(UIImage(named: "compose_photo_close"), forState: UIControlState.Normal)
        return button
    }()
    
    
}




